package cn.com.headfree.welfare.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.welfare.entity.FlNextyear;
import cn.com.headfree.welfare.entity.FlNextyear;
import cn.com.headfree.welfare.service.FlNcsgsxdjhService;
import cn.com.headfree.welfare.service.FlNextyearService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 下一年度申报计划
 * @author makejava
 * @since 2021-12-19 15:14:50
 */
@RestController
@RequestMapping("flNextyear")
public class FlNextyearController extends ExBaseController {

    private static final Logger LOG = LogManager.getLogger(FlNextyearController.class);
    /**
     * 服务对象
     */
    @Resource
    private FlNextyearService flNextyearService;
    @Resource
    private FlNcsgsxdjhService flNcsgsxdjhService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ResultData selectOne(Integer id, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            FlNextyear flNextyear=this.flNextyearService.queryById(id);
            if(flNextyear!=null)
            {
                result.setSuccess(true);
                result.setData(flNextyear);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("查询失败");
            }


        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 新增数据
     *
     * @param flNextyear 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public ResultData insert(@RequestBody FlNextyear flNextyear, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        //获取当前登录用户所在组织机构的一级部门
        String deptid=get3DeptId(user.getBaseOrgId());
        if(deptid==null){
            result.setSuccess(false);
            result.setErrorMsg("当前登录人部门信息获取异常!");
            return result;
        }
        user.setBaseOrgId(deptid);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            //flNextyear.setOrgId(user.getBaseOrgId());
            flNextyear.setCreaterId(user.getId());
            flNextyear.setCreaterName(user.getName());
            flNextyear.setCreateTime(new Date());
            flNextyear.setDelSign(0);
            flNextyear=this.flNextyearService.insert(flNextyear);
            if(flNextyear!=null)
            {
                result.setSuccess(true);
                result.setData(flNextyear);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("添加失败");
            }


        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param flNextyear 实例对象
     * @return 实例对象
     */
    @PostMapping("update")
    public ResultData update(@RequestBody FlNextyear flNextyear, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            //flNextyear.setOrgId(user.getBaseOrgId());
            flNextyear.setUpdaterId(user.getId());
            flNextyear.setUpdaterName(user.getName());
            flNextyear.setCreateTime(new Date());
            flNextyear=this.flNextyearService.update(flNextyear);
            if(flNextyear!=null)
            {
                result.setSuccess(true);
                result.setData(flNextyear);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @PostMapping("deleteById")
    public ResultData deleteById(Integer id, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            FlNextyear flNextyear=this.flNextyearService.queryById(id);
            if(flNextyear==null){
                result.setSuccess(false);
                result.setData("未查询到数据");
                return result;
            }
            //flNextyear.setOrgId(user.getBaseOrgId());
            flNextyear.setUpdaterId(user.getId());
            flNextyear.setUpdaterName(user.getName());
            flNextyear.setCreateTime(new Date());
            flNextyear.setDelSign(1);
            flNextyear=this.flNextyearService.update(flNextyear);
            if(flNextyear!=null)
            {
                result.setSuccess(true);
                result.setData(flNextyear);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }


    /**
     * 通过实体作为筛选条件查询
     *
     * @param flNextyear 实例对象
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public ResultData queryAll( @RequestBody FlNextyear flNextyear, HttpServletRequest request) {
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            PageInfo<FlNextyear> list=this.flNextyearService.queryAll(flNextyear);
            if(flNextyear!=null)
            {
                result.setSuccess(true);
                result.setData(list);
            }else {
                result.setSuccess(false);
                result.setErrorMsg("修改失败");
            }
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    /**
     * 数据批量导入
     */
    @PostMapping("/importDatas")
    public ResultData importDatas(HttpServletRequest request, HttpServletResponse response, MultipartFile file,String year,String orgId){
        ResultData result = new ResultData();
        User user = this.getLoginUserInfo(request);
        if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
            result=this.flNextyearService.importRollExcel(file,request,response,user,year,orgId);
        }else {
            LOG.error("获取统一权限用户信息异常");
            result.setSuccess(false);
            result.setErrorMsg("获取统一权限用户信息异常");
        }
        return result;
    }

    private String get3DeptId(String orgId){
        TOrgDept depts = flNcsgsxdjhService.getPdeptid(orgId);
        String re="";
        if(depts==null){
            return null;
        }
        if(Integer.valueOf(depts.getLevel())>2){
            re=get3DeptId(depts.getpId());
        }else {
            if(depts.getName().indexOf("公司")<0){
                re= get3DeptId(depts.getpId());
            }else {
                re=depts.getId();
            }
        }
        return re;
    }


}
